package com.ton.common.service;

import com.kypj.frame.db.MyTemplate;
import com.ton.common.entity.FeeeApp;
import com.ton.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Service
public class FeeeAppService {

    @Autowired
    private MyTemplate myTemplate;



    /**
     * 新增数据
     * @param feeeApp
     */
    public void saveFeeeApp(FeeeApp feeeApp){
        feeeApp.setId(StringUtil.getId()); //生成ID
        myTemplate.mySaveObject(feeeApp);
    }
    /**
     * 根据 key 查询FeeeApp
     * @param key
     */
    public FeeeApp getFeeeAppByKey(String key){
      return myTemplate.myGetObjectAppendWhere(FeeeApp.class, "app_key = ? limit 1", key);
    }

    public FeeeApp getAppById(String id){
        return myTemplate.myGetObjectAppendWhere(FeeeApp.class,"id = ? limit 1",id);
    }

    /**
     * 根据ID更新对象
     * @param feeeApp 更新对象
     */

    public void updateAppById(FeeeApp feeeApp) {
        myTemplate.myUpdateObject(feeeApp);
    }


    /**
     * 批量删除
     * @param ids ids
     */
    @Transactional
    public void batchDelete(String ids){
        String[] split = ids.trim().split(",");
        for (int i = 0; i < split.length; i++) {
            myTemplate.myDeleteObjectByPK(FeeeApp.class,split[i]);
        }
    }


    /**
     * 获取主账户
     * @return
     */
    public FeeeApp getMainApp(){
        List<FeeeApp> feeeApps = myTemplate.myQueryForListAppendWhereCondition(FeeeApp.class,"is_main_app = 1");
        if(Objects.nonNull(feeeApps)){
            return feeeApps.get(0);
        }
        return new FeeeApp();
    }
}
